package com.beiming.kotlindemo.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import com.beiming.kotlindemo.Fruit
import com.beiming.kotlindemo.R
import com.beiming.kotlindemo.adapter.FruitAdapter
import kotlinx.android.synthetic.main.activity_listview.*

class ListviewActivity : BaseActivity() {

    override fun getContentResId() = R.layout.activity_listview

    override fun initView() {
        /**
         * 没用到的参数可以用_替代
         */
        listview.setOnItemClickListener { _, _, position, _ ->
            val fruit = fruits[position]
            Toast.makeText(this, fruit.fruit_name, Toast.LENGTH_SHORT).show()
        }
    }

    private val data = listOf(
        "Apple",
        "Banana",
        "Orange",
        "Watermelon",
        "Pear",
        "Grape",
        "Pineapple",
        "Strawberry",
        "Cherry",
        "Mango",
        "Apple",
        "Banana",
        "Orange",
        "Watermelon",
        "Pear",
        "Grape",
        "Pineapple",
        "Strawberry",
        "Cherry",
        "Mango"
    )


    override fun initData() {
//        val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)
//        listview.adapter = adapter

        initListView()
    }

    private fun initListView() {
        initFruits()
        val adapter = FruitAdapter(this, R.layout.item_listview, fruits)
        listview.adapter = adapter
    }

    private val fruits = ArrayList<Fruit>()

    /**
     * repeat函数
     */
    private fun initFruits() {
        repeat(4) {
            fruits.add(Fruit("Apple", R.drawable.apple_pic))
            fruits.add(Fruit("Banana", R.drawable.banana_pic))
            fruits.add(Fruit("Orange", R.drawable.orange_pic))
            fruits.add(Fruit("Watermelon", R.drawable.watermelon_pic))
            fruits.add(Fruit("Grape", R.drawable.grape_pic))
            fruits.add(Fruit("Pineapple", R.drawable.pineapple_pic))
            fruits.add(Fruit("Strawberry", R.drawable.strawberry_pic))
            fruits.add(Fruit("Cherry", R.drawable.cherry_pic))
            fruits.add(Fruit("Mango", R.drawable.mango_pic))
        }
    }
}
